<template>
    <!-- 对话框表单 -->
    <el-dialog
        class="ba-operate-dialog"
        :close-on-click-modal="false"
        :model-value="['Add', 'Edit'].includes(baTable.form.operate!)"
        @close="baTable.toggleForm"
        width="50%"
    >
        <template #header>
            <div class="title" v-drag="['.ba-operate-dialog', '.el-dialog__header']" v-zoom="'.ba-operate-dialog'">
                {{ baTable.form.operate ? t(baTable.form.operate) : '' }}
            </div>
        </template>
        <el-scrollbar v-loading="baTable.form.loading" class="ba-table-form-scrollbar">
            <div
                class="ba-operate-form"
                :class="'ba-' + baTable.form.operate + '-form'"
                :style="'width: calc(100% - ' + baTable.form.labelWidth! / 2 + 'px)'"
            >
                <el-form
                    v-if="!baTable.form.loading"
                    ref="formRef"
                    @submit.prevent=""
                    @keyup.enter="baTable.onSubmit(formRef)"
                    :model="baTable.form.items"
                    label-position="right"
                    :label-width="baTable.form.labelWidth + 'px'"
                    :rules="rules"
                >
                    <FormItem
                        :label="t('xmwms.product.product_no')"
                        type="string"
                        v-model="baTable.form.items!.product_no"
                        prop="product_no"
                        :placeholder="t('Please input field', { field: t('xmwms.product.product_no') })"
                    />
                    <FormItem
                        :label="t('xmwms.product.bar_code')"
                        type="string"
                        v-model="baTable.form.items!.bar_code"
                        prop="bar_code"
                        :placeholder="t('Please input field', { field: t('xmwms.product.bar_code') })"
                    />
                    <FormItem
                        :label="t('xmwms.product.name')"
                        type="string"
                        v-model="baTable.form.items!.name"
                        prop="name"
                        :placeholder="t('Please input field', { field: t('xmwms.product.name') })"
                    />
                    <FormItem
                        :label="t('xmwms.product.brand_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.brand_id"
                        prop="brand_id"
                        :input-attr="{ pk: 'brand.id', field: 'name', 'remote-url': '/admin/xmwms.Brand/index' }"
                        :placeholder="t('Please select field', { field: t('xmwms.product.brand_id') })"
                    />
                    <FormItem
                        :label="t('xmwms.product.category_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.category_id"
                        prop="category_id"
                        :input-attr="{
                            multiple: false,
                            params: { isTree: true },
                            pk: 'category.id',
                            field: 'name',
                            'remote-url': '/admin/xmwms.Category/index',
                        }"
                        :placeholder="t('Please select field', { field: t('xmwms.product.category_id') })"
                    />
                    <FormItem
                        :label="t('xmwms.product.unit_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.unit_id"
                        prop="unit_id"
                        :input-attr="{ pk: 'unit.id', field: 'name', 'remote-url': '/admin/xmwms.Unit/index' }"
                        :placeholder="t('Please select field', { field: t('xmwms.product.unit_id') })"
                    />
                    <FormItem
                        :label="t('xmwms.product.spec')"
                        type="string"
                        v-model="baTable.form.items!.spec"
                        prop="spec"
                        :placeholder="t('Please input field', { field: t('xmwms.product.spec') })"
                    />
                    <FormItem
                        :label="t('xmwms.product.price')"
                        type="number"
                        prop="price"
                        :input-attr="{ step: 1 }"
                        v-model.number="baTable.form.items!.price"
                        :placeholder="t('Please input field', { field: t('xmwms.product.price') })"
                    />
                    <FormItem
                        :label="t('xmwms.product.safety_stock_num')"
                        type="number"
                        prop="safety_stock_num"
                        :input-attr="{ step: 1 }"
                        v-model.number="baTable.form.items!.safety_stock_num"
                        :placeholder="t('Please input field', { field: t('xmwms.product.safety_stock_num') })"
                    />
                    <FormItem
                        :label="t('xmwms.product.warehouse_shelf_id')"
                        type="remoteSelect"
                        v-model="baTable.form.items!.warehouse_shelf_id"
                        prop="id"
                        :input-attr="{
                            multiple: false,
                            params: { isTree: true },
                            pk: 'id',
                            field: 'name',
                            'remote-url': '/admin/xmwms.Warehouse/index',
                        }"
                        :placeholder="t('Please select field', { field: t('xmwms.product.warehouse_shelf_id') })"
                    />
                    <FormItem :label="t('xmwms.product.images')" type="images" v-model="baTable.form.items!.images" prop="images" />
                    <FormItem
                        :label="t('xmwms.product.weigh')"
                        type="number"
                        prop="weigh"
                        :input-attr="{ step: 1 }"
                        v-model.number="baTable.form.items!.weigh"
                        :placeholder="t('Please input field', { field: t('xmwms.product.weigh') })"
                    />
                    <FormItem
                        :label="t('xmwms.product.status')"
                        type="radio"
                        v-model="baTable.form.items!.status"
                        prop="status"
                        :data="{ content: { 10: t('xmwms.product.status 10'), 20: t('xmwms.product.status 20') } }"
                        :placeholder="t('Please select field', { field: t('xmwms.product.status') })"
                    />
                    <FormItem
                        :label="t('xmwms.product.effective_day')"
                        type="number"
                        v-model="baTable.form.items!.effective_time"
                        prop="effective_day"
                        :placeholder="t('Please select field', { field: t('xmwms.product.effective_day') })"
                    />
                    <FormItem
                        :label="t('xmwms.product.remark')"
                        type="textarea"
                        v-model="baTable.form.items!.remark"
                        prop="remark"
                        :placeholder="t('Please input field', { field: t('xmwms.product.remark') })"
                    />
                </el-form>
            </div>
        </el-scrollbar>
        <template #footer>
            <div :style="'width: calc(100% - ' + baTable.form.labelWidth! / 1.8 + 'px)'">
                <el-button @click="baTable.toggleForm('')">{{ t('Cancel') }}</el-button>
                <el-button v-blur :loading="baTable.form.submitLoading" @click="baTable.onSubmit(formRef)" type="primary">
                    {{ baTable.form.operateIds && baTable.form.operateIds.length > 1 ? t('Save and edit next item') : t('Save') }}
                </el-button>
            </div>
        </template>
    </el-dialog>
</template>

<script setup lang="ts">
import { reactive, ref, inject } from 'vue'
import { useI18n } from 'vue-i18n'
import type baTableClass from '/@/utils/baTable'
import FormItem from '/@/components/formItem/index.vue'
import type { ElForm, FormItemRule } from 'element-plus'
import { buildValidatorData } from '/@/utils/validate'

const formRef = ref<InstanceType<typeof ElForm>>()
const baTable = inject('baTable') as baTableClass

const { t } = useI18n()

const rules: Partial<Record<string, FormItemRule[]>> = reactive({
    name: [buildValidatorData({ name: 'required', title: t('xmwms.product.name') })],
    spec: [buildValidatorData({ name: 'required', title: t('xmwms.product.spec') })],
    category_id: [buildValidatorData({ name: 'required', title: t('xmwms.product.category_id') })],
    unit_id: [buildValidatorData({ name: 'required', title: t('xmwms.product.unit_id') })],
    safety_stock_num: [buildValidatorData({ name: 'required', title: t('xmwms.product.safety_stock_num') })],
    effective_day: [buildValidatorData({ name: 'date', title: t('xmwms.product.effective_day') })],
})
</script>

<style scoped lang="scss"></style>
